package org.whb.pet.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.whb.basic.service.impl.BaseServiceImpl;
import org.whb.basic.util.CodeGenerateUtils;
import org.whb.basic.util.DistanceUtil;
import org.whb.basic.util.PageList;
import org.whb.basic.util.Point;
import org.whb.order.domain.PetAcquisitionOrder;
import org.whb.order.mapper.PetAcquisitionOrderMapper;
import org.whb.org.domain.Employee;
import org.whb.org.domain.Shop;
import org.whb.org.mapper.EmployeeMapper;
import org.whb.org.mapper.ShopMapper;
import org.whb.org.service.IEmployeeService;
import org.whb.pet.domain.Pet;
import org.whb.pet.domain.PetDetail;
import org.whb.pet.domain.SearchMasterMsg;
import org.whb.pet.mapper.SearchMasterMsgMapper;
import org.whb.pet.query.SearchMasterMsgQuery;
import org.whb.pet.service.IPetDetailService;
import org.whb.pet.service.IPetService;
import org.whb.pet.service.ISearchMasterMsgService;
import org.whb.user.domain.Logininfo;
import org.whb.user.domain.User;
import org.whb.user.mapper.UserMapper;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private IPetService petService;
    @Autowired
    private PetAcquisitionOrderMapper petAcquisitionOrderMapper;


    @Override
    public void publish(SearchMasterMsg searchMasterMsg, Logininfo logininfo) {
        //1、通过logininfo查找user
        User user=userMapper.loadByLogininfoId(logininfo.getId());
        //2、找到离user最近的店铺
        String address = searchMasterMsg.getAddress();
        Point point = DistanceUtil.getPoint(address);
        List<Shop> shops = shopMapper.loadAll();
        Shop nearestShop = DistanceUtil.getNearestShop(point, shops);
        //将shopid和userid赋值给searchMasterMsg
        searchMasterMsg.setShop_id(nearestShop.getId());
        searchMasterMsg.setUser_id(user.getId());
        //保存searchMasterMsg
        searchMasterMsgMapper.save(searchMasterMsg);
    }

    /**
     * 未处理查询
     * @param query
     * @param logininfo
     * @return
     */
    @Override
    public PageList<SearchMasterMsg> pending(SearchMasterMsgQuery query, Logininfo logininfo) {
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());

        query.setShopId(employee.getShop_id());

        query.setState(0);

        return super.queryPage(query);
    }

    @Override
    public PageList<SearchMasterMsg> processed(SearchMasterMsgQuery query, Logininfo logininfo) {
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());

        query.setShopId(employee.getShop_id());

        query.setState(1);

        return super.queryPage(query);
    }

    /**
     * 处理寻主消息
     * @param pet
     * @param logininfo
     */
    @Override
    public void handle(Pet pet, Logininfo logininfo) {
        petService.add(pet);
        //将未处理寻主改为已处理
        searchMasterMsgMapper.updateState2ProcessedById(pet.getSearch_master_msg_id());

        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.loadById(pet.getSearch_master_msg_id());

        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        PetAcquisitionOrder petAcquisitionOrder = pet2order(pet, searchMasterMsg, employee.getId());
        petAcquisitionOrderMapper.save(petAcquisitionOrder);
    }


    private PetAcquisitionOrder pet2order(Pet pet, SearchMasterMsg searchMasterMsg,Long empId) {
        PetAcquisitionOrder order = new PetAcquisitionOrder();
        order.setDigest("[摘要]对"+pet.getName()+"收购订单！");
        order.setState(0);//待支付
        order.setPrice(pet.getCostprice());
        order.setAddress(searchMasterMsg.getAddress());
        String orderSn = CodeGenerateUtils.generateOrderSn(searchMasterMsg.getUser_id());
        order.setOrderSn(orderSn);
        order.setPet_id(pet.getId());
        order.setUser_id(searchMasterMsg.getUser_id());
        order.setPaytype(pet.getPayType());
        order.setShop_id(pet.getShop_id());
        order.setEmp_id(empId);
        return order;
    }
}
